<template>
  <div class="content-wrapper">
    <div class="content-header">我的记录</div>
    <div class="content-container">
    <el-form :inline="true" :model="listQuery" class="filter-container">
      <el-form-item label="产品项目">
        <el-cascader
          ref="cascader"
          v-model="ProducValue"
          :options="ProductList"
          @change="change"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="批号">
        <el-input v-model="listQuery.batchCode" placeholder="输入批号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          value="value"
          class="filter-item"
          size="small"
          round
          type="primary"
          icon="el-icon-search"
          @click="getDataList"
        >
          搜索
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      height="500"
      v-loading="listLoading"
      :data="list"
      highlight-current-row
    >
      <el-table-column
        label="产品项目"
        prop="productDictValue"
        min-width="100px"
      />
      <el-table-column
        label="产品子项"
        prop="subProductDictValue"
        min-width="100px"
      >
        <template slot-scope="{ row }">
          {{ row.subProductDictValue || '没有子项' }}
        </template>
      </el-table-column>
      <el-table-column
        label="生产工艺"
        prop="techDocumentName"
        min-width="100px"
      />
      <el-table-column
        label="批号"
        prop="batchCode"
        width="100px"
        align="center"
      />
      <el-table-column label="开始时间" prop="createTime" width="160px" />
      <el-table-column label="结束时间" prop="updateTime" width="160px" />
      <el-table-column
        label="状态"
        prop="status"
        width="100px"
        :formatter="
          (row, column, cellValue, index) => {
            return status[cellValue]
          }
        "
      />
      <el-table-column label="操作" align="center" width="80px" fixed="right">
        <template slot-scope="{ row }">
          <el-dropdown trigger="click" class="filter-item">
            <el-button type="text" class="el-icon-more" />
            <el-dropdown-menu slot="dropdown" class="user-dropdown">
              <el-dropdown-item @click.native="editDocument(row)"
              v-if="row.status==1"
                >编辑</el-dropdown-item
              >
              <el-dropdown-item @click.native="pushProduction(row)"
                >提交批生产记录</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page-number.sync="listQuery.pageNumber"
      :page-size.sync="listQuery.pageSize"
      @pagination="getDataList"
    />

    <SelectPersonnel
      :SelectPersonnelData="SelectPersonnelData"
      v-on:receiveUserData="receiveUserData"
    ></SelectPersonnel>
    </div>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import {
  recordPage,
  pushApplyForData,
  pushProduction
} from '@/api/rs/manufacture.js'
import SelectPersonnel from '@/components/SelectPersonnel/index.vue'
import { listDictTreeItem } from '@/api/base/dict.js'
import { get } from '@/api/rs/technique'

export default {
  components: { Pagination, SelectPersonnel },
  data() {
    return {
      status: {
        0: '未记录',
        1: '记录中',
        2: '记录异常',
        3: '提交修改',
        4: '提交批生产记录',
      },
      selectvalue: '',
      value: '',
      listLoading: false,
      list: [{}],
      total: 8,
      listQuery: {
        pageNumber: 1,
        pageIndex: 0,
        pageSize: 10,
        subProductDictCode: '',
        batchCode: ''
      },
      SelectPersonnelData: {},
      manufactureRecordId: '',
      ProductList: [],
      ProducValue: []
    }
  },
  mounted() {
    this.getDataList()
    this.listDictTreeItem()
  },
  methods: {
    change(val){
      this.listQuery.subProductDictCode = val[1]
    },
    listDictTreeItem(){
      listDictTreeItem({
        dictGroupCode: 'product',
        parentDictId: 0
      }).then(res => {
        this.ProductList = res.data
      })
    },
    /**
     * 编辑记录文件
     * @param row
     */
    editDocument(row){
      console.log(row, 'row')
      get({techniqueId: row.techniqueId}).then(res => {
        if (res.code == 1) {
          window.open(
            'onlyoffice/recordDocument.html?&token=' +
            this.$store.getters.token +
            '&documentId=' +
            row.dataRecordId +
            '&mode=edit' +
            '&pefDocumentId=' + res.data.documentId +
            '&urldata=' +
            process.env.VUE_APP_BASE_API
          )
        }else{
          this.$message.error("图纸信息获取失败")
        }
      })

    },
    getDataList() {
      this.listQuery.pageIndex = this.listQuery.pageNumber - 1
      recordPage(this.listQuery).then((res) => {
        this.list = res.data.rows
        this.total = res.data.total
      })
    },
    receiveUserData(data) {
      this.from.receiveUserId = data.receiveUserId
      this.from.receiveUserName = data.receiveUserName
    },
    pushProduction(row) {

      let data = {
        batchCode: row.batchCode,
        manufactureRecordId: row.manufactureRecordId,
        productDictCode: row.productDictCode,
        subProductDictCode: row.subProductDictCode
      }
      pushProduction(data).then((res) => {
        if (res.code == 1) {
          this.$message({
            message: '提交成功',
            type: 'success'
          })
          this.getDataList()
        }
      })
    }
  }
}
</script>
